//
//  YJXTitleCollectionView.m
//  IntegralMall
//
//  Created by 易金鑫 on 2017/10/16.
//  Copyright © 2017年 Toocms. All rights reserved.
//

#import "YJXTitleCollectionView.h"
#import "YJXCollectionViewTitleCollectionViewCell.h"



@interface YJXTitleCollectionView()< UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>

@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,assign)NSInteger count;


@end
@implementation YJXTitleCollectionView

-(instancetype)initWithFrame:(CGRect)frame TitleArray:(NSArray *)titleArray CountOfLine:(NSInteger) countOfLine{

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = Graycolor;
        self.titleArray = titleArray;
        self.count = countOfLine;
        [self createUI];
    }
    return self;
}

-(void)createUI{

    UICollectionViewFlowLayout *flowout = [UICollectionViewFlowLayout new];
    flowout.minimumLineSpacing = 0;
    flowout.minimumInteritemSpacing = 0;
    flowout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)collectionViewLayout:flowout];

    _collectionView.delegate = self;
    _collectionView.dataSource = self;
//    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = Graycolor;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[YJXCollectionViewTitleCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self addSubview:_collectionView];
    [self.collectionView reloadData];
}
- (void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex=selectIndex;
    [self scrollToSelectIndex];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _titleArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YJXCollectionViewTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

//    cell.rightLineView.hidden = _selectIndex == self.titleArray.count - 1 ? YES : NO;
    cell.bottomLineView.hidden = _selectIndex == indexPath.row ? NO : YES;
    cell.bottomLineView.backgroundColor = _selectIndex == indexPath.row ? ThemeColor : HexColor(@"4c4c4c");

    cell.titleLabel.text = _titleArray[indexPath.row];
    cell.titleLabel.textColor = _selectIndex == indexPath.row ? ThemeColor : [UIColor blackColor];

    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(kScreenWidth / _count, self.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    _selectIndex = indexPath.row;
    [self.collectionView reloadData];
    [self scrollToSelectIndex];

    [self.delegate didSelectItem:indexPath.row];
}


-(void)scrollToSelectIndex{

    if (self.count % 2 == 0) {

        if (_selectIndex > self.count / 2 && _selectIndex < self.titleArray.count - (self.count / 2)) {

            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / _count * ( _selectIndex - self.count / 2 ), 0 )];
            }];
        }

        if (_selectIndex <= self.count / 2 ) {

            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(0, 0 )];
            }];
        }

        if ( _selectIndex >= self.titleArray.count - (self.count / 2)) {

            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / _count * ( self.titleArray.count - self.count), 0 )];
            }];
        }

    }else{

        if (_selectIndex > self.count / 2 - 1 && _selectIndex < self.titleArray.count - (self.count / 2 + 1)) {

            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / _count * ( _selectIndex - self.count / 2), 0 )];
            }];
        }

        if (_selectIndex <= self.count / 2 - 1){

            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(0, 0 )];
            }];

        }


        if (_selectIndex >= self.titleArray.count - (self.count / 2 + 1)){

            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / _count * (self.titleArray.count - self.count), 0 )];
            }];
            
        }
    }

}
@end
